package paintball;

import game.GameMap;
import game.GameObject;
import game.PlayerController;

import java.net.Socket;
import java.util.ArrayList;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.KeyListener;
import org.newdawn.slick.SlickException;

import server.*;

public class PaintballGame extends BasicGame implements KeyListener {
	// Display properties
	public static int screenHeight = 600;
	public static int screenWidth = 800;
	private static boolean fullscreen = false;
	
	// Game map where stuff is drawn on
	private GameMap map = new GameMap(screenWidth, screenHeight);
	
	// Player connections
	private ArrayList<Connection> connections = new ArrayList<Connection>();
	
	// Images
	private Image projectileimg;
	private Image playerimg;
	
	public PaintballGame() {
		super("Twisty! Paintball");
	}
	
	// Player List
	public ArrayList<PaintballPlayer> players(){
		return PlayerController.getPlayers();
	}

	public static void main(String[] args) throws SlickException {
		PaintballGame game = new PaintballGame();
		AppGameContainer app = new AppGameContainer(game);
		app.setDisplayMode(screenWidth, screenHeight, fullscreen);
		app.setTargetFrameRate(60);
		
		Server s = new Server(game);
		
		app.start();
	}
	
	@Override
	public void render(GameContainer arg0, Graphics g) throws SlickException {
		try{
			if(players().size() > 0){
				for(PaintballPlayer p: players()){
					//if(p.isActive()){
						this.playerimg = new Image(p.curImgPath());
						g.rotate((p.getX()+(p.getWidth()/2)), (p.getY()+(p.getHeight()/2)), (float) p.getRotation());
						g.drawImage(this.playerimg, p.getX(), p.getY());
						g.resetTransform();
						g.drawString(p.getPlayerName(), p.getX(), p.getY()+p.getHeight());
						if(p.bullet != null && p.bullet.isActive()){
							this.projectileimg = new Image(p.bullet.curImgPath());
							g.drawImage(this.projectileimg, p.bullet.getX(), p.bullet.getY());
						}
					//}
				}
			}
		}catch(Exception e){
			return;
		}
	}

	@Override
	public void init(GameContainer arg0) throws SlickException {
		//PlayerController.addPlayer("test", 20, 20, new Socket());
		PlayerController.addPlayer("henk", 180, 180, new Socket());
		PlayerController.addPlayer("arie", 180, 380, new Socket());
		//PlayerController.addPlayer("toos", 280, 480, new Socket());
		this.playerimg = new Image("res/paintballplayer.gif");
		this.projectileimg = new Image("res/projectile.png");
	}

	@Override
	public void update(GameContainer arg0, int arg1) throws SlickException {
		try{
			for(PaintballPlayer p: players()){
				if(p.state == GameObject.State.DEAD){
					p.currentRespawnTime--;
					if(p.currentRespawnTime < 0){
						p.Respawn(map);
						p.currentRespawnTime = p.getRespawnTime();
					}
				}
				if(p.isActive() && p.move){
					p.move(map);
				}
				if(p.bullet != null){
					p.bullet.step(map, players());
				}
			}
		}catch(Exception e){
			System.out.println("asd");
			return;
		}
	}
	
	@Override
	public void keyPressed(int key, char c){
		if(key == Input.KEY_Z){
			this.players().get(0).moveToCurrentDirection();
		}
		if(key == Input.KEY_X){
			this.players().get(0).addRotation(5);
		}
		if(key == Input.KEY_C){
			this.players().get(0).shoot();
		}
		if(key == Input.KEY_S){
		}
	}
	
	public void addConnection(Connection c) {
		this.connections.add(c);
	}
}
